package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.vo.page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//RestController接口发JSON
@RestController

@RequestMapping("/user")
public class UserController {

    @Resource
    UserMapper userMapper;

    @GetMapping("/all")
    public List<User> getUser(){
        return userMapper.findAll();
    }
    @GetMapping("/select/{id}")
    public User getUserById(@PathVariable("id") Integer id){
         return userMapper.selectById(id);
    }
    @PostMapping("/save")
    public String save(@RequestBody User user){
        userMapper.save(user);
        return "success";
    }

    @PutMapping("/update")
    public String update(@RequestBody User user){
        userMapper.update(user);
        return "success";
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
       userMapper.delete(id);
       return "success";
    }

    @GetMapping("/page")
    public page<User> findPageById(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        Integer offest  = (pageNum - 1)*pageSize;
        page<User> page = new page<>();

        List<User> userData = userMapper.findPage(offest,pageSize);
        page.setData(userData);

        Integer count = userMapper.countuser();
        page.setTotal(count);

        page.setPageNum(pageNum);
        page.setPageSize(pageSize);

        return page;
    }
}
